package com.course.finalproject.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import com.course.finalproject.Constants;
import com.course.finalproject.MyApplication;
import com.course.finalproject.R;
import com.course.finalproject.Utility;
import com.course.finalproject.adapter.WeatherArrayAdapter;
import com.course.finalproject.model.Weather;

/**
 * Class that represent the weather activity.
 * @author	Mariano Bessone <mariano.bessone @ tallertechnologies.com>
 * @since	2012-09-07
 */
public class WeatherActivity extends ListActivity {
	
	/** Adapter. */
	private WeatherArrayAdapter weatherArrayAdapter;
	/** Loading progress dialog. */
	private ProgressDialog progressDialog;
	
	/** Local broadcast receiver to update the weather information. */
	private BroadcastReceiver weatherReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			// If the list of weather is not empty extract it and copy to local list.
			List<Weather> weathers = intent.getParcelableArrayListExtra(Constants.WEATHER_LIST_PARCEL_KEY);
			if ( !weathers.isEmpty() ) {
				weatherArrayAdapter.clear();
				for (Weather weather : weathers) {
	 				weatherArrayAdapter.add(weather);
				}
			} else
				Utility.longToast("The weather could not be retrieved.\nTry again from Menu->Refresh.");
			// Dismiss the progress dialog.
			progressDialog.dismiss();
    	}
	};
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		Log.d(Constants.LOG_TAG, "WeatherActivity.onCreate()");
		// Super call.
        super.onCreate(savedInstanceState);
        // Create loading progress dialog and add view settings.
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.setMessage("Updating weather ...");
		progressDialog.setCancelable(false);
        // Set the list adapter.
		weatherArrayAdapter = new WeatherArrayAdapter(this, new ArrayList<Weather>());
        setListAdapter(weatherArrayAdapter);
        // Show progress dialog
 		progressDialog.show();
        // Register to receive a new weather update.
  		LocalBroadcastManager.getInstance(this).registerReceiver(weatherReceiver, new IntentFilter(Constants.NEW_WEATHER_UPDATE));
        // If the weather is already retrieved then fill the adapter with it.
        MyApplication.getInstance().getWeatherManager().askForNewWeather();
    }

	@Override
	protected void onDestroy() {
		// Unregister to do not receive a new battery log from now.
		LocalBroadcastManager.getInstance(this).unregisterReceiver(weatherReceiver);
		// Super call.
		super.onDestroy();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.weather_list_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection.
	    switch (item.getItemId()) {
	        case R.id.weather_list_menu_refresh:
	        	// Executes a request to get a new weather forecast list.
	        	MyApplication.getInstance().getWeatherManager().doWeatherRequest();
	        	progressDialog.show();
	            return true;
	        default:
	        	// Super handler.
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		Intent intent = new Intent(this, WeatherItemActivity.class);
		intent.putExtra(Constants.WEATHER_PARCEL_KEY, weatherArrayAdapter.getItem(position));
		startActivity(intent);
	}
	
}
